<script type="text/javascript">
function validate_form() {
    var name = $('#name').val();
    var code = $('#code').val();
    var image = $('#image').val();
    var price = $('#price').val();
    var size = $('#size').val();
    var countError = 0;
    
    if((name.length < 1) || (name.length > 100)) {
    	$('#errorName').html('Tên sản phẩm từ 1-100 kí tự');
        countError++;
    }
    else {
        $('#errorName').html('');
    }
    if((code.length < 1) || (code.length > 100)) {
    	$('#errorCode').html('Mã sản phẩm từ 1-100 kí tự');
        countError++;
    }
    else {
        $('#errorCode').html('');
    }
    if(image != '') {
        if(!/(\.png|\.gif|\.jpg|\.jpeg\.PNG|\.GIF|\.JPG|\.JPEG)$/i.test(image)) {
            $('#errorImage').html('Ảnh phải đúng định dạng jpg, jpeg, png, gif');
            countError++; 
        }
        else {
            $('#errorImage').html('');
        }
    }
    var reg = /^([0-9]{1,10})$/;
    if(!reg.test(price)) {
        $('#errorPrice').html('Giá tiền từ 1-10 kí tự số');
        countError++;
    }
    else {
        $('#errorPrice').html('');
    }
    if(size != '') {
        if(size.length > 100) {
            $('#errorSize').html('Kích thước không quá 100 kí tự');
            countError++;
        }
        else {
            $('#errorSize').html('');
        }
    }
    
    if(countError > 0) {
        return false;
    }
    else {
        return true;
    }
}
</script>
    <div class="list_category">
        <ul>
            <li><a href="<?php echo $this->baseUrl('admincp/product'); ?>">Quản lý sản phẩm</a></li>
            <li><a href="<?php echo $this->baseUrl('admincp/product/create'); ?>">Thêm sản phẩm mới</a></li>
        </ul>
    </div>
</div>

<!-- Begin Content -->
<div class="section">
    <div class="heading_section">
        <span>Sửa thông tin sản phẩm</span>
    </div>
    
    <div class="content">
    <form action="" method="post" enctype="multipart/form-data" onsubmit="return validate_form()">
        <table class="table">
            <tr>
                <td class="td1"><?php echo $this->formLabel('name','Tên sản phẩm'); ?>&nbsp;<span class="error-msg">*</span></td>
                <td class="td2" colspan="2">
                    <?php echo $this->formText('name',$this->data['name'],array('class'=>'input')); ?>
                    <span class="error-msg" id="errorName"><?php echo $this->errorName; ?></span>
                </td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('code','Mã sản phẩm'); ?>&nbsp;<span class="error-msg">*</span></td>
                <td class="td2" colspan="2">
                    <?php echo $this->formText('code',$this->data['code'],array('class'=>'input')); ?>
                    <span class="error-msg" id="errorCode"><?php echo $this->errorCode; ?></span>
                </td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('category','Danh mục sản phẩm'); ?>&nbsp;<span class="error-msg">*</span></td>
                <td class="td2">
                    <select name="category" id="category" class="selectbox">
                    <?php
                        if(count($this->category) > 0) :
                            $category = new Admincp_Model_Category();
                            foreach($this->category as $value) :
    							$levelCss = '';
    				            if($value['level'] == 0){
    				            	continue;
    				            }else if($value['level'] == 1){
    				            	$name = $value['name'];
    				            }else{
    				            	$name = ' - - ' . $value['name'];
    				            	$levelCss =  'padding-left: ' . (10 * $value['level']) . 'px;';
    				            }
			            ?>
                        <?php if($category->checkHasChild($value['id']) > 0) : ?>
						<option disabled="disabled" value="<?php echo $value['id']; ?>" style="<?php echo $levelCss; ?>"><?php echo $name; ?></option>
                        <?php else : ?>
                        <?php if($this->data['category_id'] == $value['id']) : ?>
                        <option selected="selected" value="<?php echo $value['id']; ?>" style="<?php echo $levelCss; ?>"><?php echo $name; ?></option>
                        <?php else : ?>
                        <option value="<?php echo $value['id']; ?>" style="<?php echo $levelCss; ?>"><?php echo $name; ?></option>
                        <?php endif; ?>
                        <?php endif; ?>
						<?php
                                endforeach;
                            endif;
                        ?>
                    </select>
                </td>
                <td class="td3">&nbsp;</td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('provider','Thương hiệu'); ?>&nbsp;<span class="error-msg">*</span></td>
                <td class="td2">
                    <select name="provider" id="provider" class="selectbox">
                        <?php
                            if(count($this->provider) > 0) :
                                foreach($this->provider as $value) :
                        ?>
                        <?php if($this->data['provider_id'] == $value['id']) : ?>
                        <option selected="selected" value="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></option>
                        <?php else : ?>
                        <option value="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></option>
                        <?php endif; ?>
                        <?php
                                endforeach;
                            endif;
                        ?>
                    </select>
                </td>
                <td class="td3">&nbsp;</td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('old_image','Ảnh sản phẩm'); ?>&nbsp;&nbsp;&nbsp;</td>
                <td class="td2" colspan="2">
                    <img src="<?php echo $this->baseUrl('/public/files/product/thumbnail/').$this->data['thumbnail']; ?>" width="80px" height="80px" />
                </td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('image','Ảnh mới (nếu có)'); ?>&nbsp;&nbsp;&nbsp;</td>
                <td class="td2" colspan="2">
                    <?php echo $this->formFile('image',array('style'=>'width:211px')); ?>
                    <span class="error-msg" id="errorImage"><?php echo $this->errorImage; ?></span>
                </td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('price','Giá tiền'); ?>&nbsp;<span class="error-msg">*</span></td>
                <td class="td2" colspan="2">
                    <?php echo $this->formText('price',$this->data['price'],array('class'=>'input','style'=>'text-align: right; width: 150px')); ?>000 VNĐ
                    <span class="error-msg" id="errorPrice"><?php echo $this->errorPrice; ?></span>
                </td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('size','Kích thước'); ?>&nbsp;&nbsp;&nbsp;</td>
                <td class="td2" colspan="2">
                    <?php echo $this->formText('size',$this->data['size'],array('class'=>'input')); ?>
                    <span class="error-msg" id="errorSize"><?php echo $this->errorSize; ?></span>
                </td>
            </tr>
            <tr>
                <td class="td1"><?php echo $this->formLabel('description','Mô tả sản phẩm'); ?>&nbsp;&nbsp;&nbsp;</td>
                <td class="td2"><?php echo $this->formTextarea('description',$this->data['description'],array('class'=>'ckeditor')); ?></td>
                <td class="td3" id="errorDescription"><?php echo $this->errorDescription; ?></td>
            </tr>
            <tr>
                <td class="td1">&nbsp;</td>
                <td class="td2">
                    <?php echo $this->formSubmit('submit','Cập nhật',array('class'=>'button')); ?>
                    <?php echo $this->formReset('reset','Nhập lại',array('class'=>'button')); ?>
                </td>
                <td class="td3"></td>
            </tr>
        </table>
    </form>
    </div>
</div>
<!-- End Content -->